package com.dynamicthreadpool.manager;

import com.dynamicthreadpool.core.DynamicThreadPool;
import com.dynamicthreadpool.dubbo.DynamicThreadPoolFactory;
import com.dynamicthreadpool.spi.URL;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;

/**
 * 线程池管理器，用于管理和更新多个动态线程池实例
 */
public class ThreadPoolManager {

    private static final ThreadPoolManager INSTANCE = new ThreadPoolManager();
    private final Map<String, Executor> threadPoolMap = new ConcurrentHashMap<>();

    private ThreadPoolManager() {
    }

    /**
     * 获取线程池管理器实例
     * @return 线程池管理器单例
     */
    public static ThreadPoolManager getInstance() {
        return INSTANCE;
    }

    /**
     * 注册线程池
     * @param key 线程池标识
     * @param executor 执行器实例
     */
    public void registerThreadPool(String key, Executor executor) {
        if (key != null && executor != null) {
            threadPoolMap.put(key, executor);
        }
    }

    /**
     * 注销线程池
     * @param key 线程池标识
     */
    public void unregisterThreadPool(String key) {
        if (key != null) {
            Executor executor = threadPoolMap.remove(key);
            if (executor instanceof DynamicThreadPool) {
                ((DynamicThreadPool) executor).shutdown();
            }
        }
    }

    /**
     * 获取线程池
     * @param key 线程池标识
     * @return 执行器实例
     */
    public Executor getThreadPool(String key) {
        return threadPoolMap.get(key);
    }

    /**
     * 根据URL动态更新线程池参数
     * @param key 线程池标识
     * @param url 包含新参数的URL
     */
    public void updateThreadPool(String key, URL url) {
        Executor executor = threadPoolMap.get(key);
        if (executor != null) {
            DynamicThreadPoolFactory.updateThreadPool(executor, url);
        }
    }

    /**
     * 根据参数动态更新线程池
     * @param key 线程池标识
     * @param coreThreads 核心线程数
     * @param maxThreads 最大线程数
     * @param queueCapacity 队列容量
     * @param keepAliveTime 保持时间
     * @param queueType 队列类型
     * @param rejectPolicy 拒绝策略
     * @param threadName 线程名称
     */
    public void updateThreadPool(String key, int coreThreads, int maxThreads, int queueCapacity,
                                long keepAliveTime, String queueType, String rejectPolicy, String threadName) {
        Executor executor = threadPoolMap.get(key);
        if (executor instanceof DynamicThreadPool) {
            DynamicThreadPool dynamicThreadPool = (DynamicThreadPool) executor;
            
            dynamicThreadPool.setCorePoolSize(coreThreads);
            dynamicThreadPool.setMaximumPoolSize(maxThreads);
            dynamicThreadPool.setQueueCapacity(queueCapacity); // 使用新的方法调整队列容量
            dynamicThreadPool.setKeepAliveTime(keepAliveTime, java.util.concurrent.TimeUnit.MILLISECONDS);
            dynamicThreadPool.setRejectPolicy(rejectPolicy);
            dynamicThreadPool.setPoolName(threadName);
        }
    }

    /**
     * 获取所有线程池的状态信息
     * @return 线程池状态信息
     */
    public Map<String, String> getAllThreadPoolStatus() {
        Map<String, String> statusMap = new ConcurrentHashMap<>();
        for (Map.Entry<String, Executor> entry : threadPoolMap.entrySet()) {
            Executor executor = entry.getValue();
            if (executor instanceof DynamicThreadPool) {
                statusMap.put(entry.getKey(), executor.toString());
            }
        }
        return statusMap;
    }

    /**
     * 关闭所有线程池
     */
    public void shutdownAll() {
        for (Executor executor : threadPoolMap.values()) {
            if (executor instanceof DynamicThreadPool) {
                ((DynamicThreadPool) executor).shutdown();
            }
        }
        threadPoolMap.clear();
    }
}
